<template>
    <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <a-form layout="inline" @keyup.enter.native="searchQuery">
                <a-row :gutter="24">
                    <a-col :md="6" :sm="8">
                        <a-form-item label="成果编号">
                            <a-input placeholder="请填写成果编号" v-model="queryParam.cxj_xmbh"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="8">
                        <a-form-item label="成果名称">
                            <a-input placeholder="请填写成果名称" v-model="queryParam.cxj_mc_zw"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="8">
                        <a-form-item label="申报单位">
                            <a-input placeholder="请填写申报单位" v-model="queryParam.sbdw_qymc"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="8">
                        <a-form-item label="推荐状态">
                            <a-select v-model="queryParam.tjzt" placeholder="请选择推荐状态">
                                <a-select-option value="">请选择</a-select-option>
                                <a-select-option value="200">待推荐</a-select-option>
                                <a-select-option value="203">同意推荐</a-select-option>
                                <a-select-option value="202">不同意推荐</a-select-option>
                                <a-select-option value="201">退回修改</a-select-option>
                                <a-select-option value="204">退回修改已提交</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                  <a-col :md="6" :sm="8">
                    <a-form-item label="初审状态">
                      <a-select v-model="queryParam.cszt" placeholder="请选择初审状态">
                        <a-select-option value="">请选择</a-select-option>
                        <a-select-option value="401">初审通过（进入复审）</a-select-option>
                        <a-select-option value="402">初审未通过</a-select-option>
                      </a-select>
                    </a-form-item>
                  </a-col>
                    <a-col :md="6" :sm="8">
                        <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                            <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                        </span>
                    </a-col>
                    <a-col :md="24" :sm="24">
                        <span style="float: left;overflow: hidden;">
                            <a-button type="primary" @click="batchRecommend">批量推荐</a-button>
                        </span>
                        <span style="float: right;overflow: hidden;" class="table-page-search-submitButtons">
                            <a-button type="primary" icon="download" style="margin-left: 8px" @click="exportTjcgXls">推荐成果汇总表下载</a-button>
                            <a-button type="primary" icon="upload" style="margin-left: 8px" @click="TjcgUpLoad">盖章上传</a-button>
                            <input type="file" style="display:none;" class="pointer" id="gzsc" accept=".pdf" value="上传" @change="handlegzsc"/>
                            <a-button type="primary" icon="download" style="margin-left: 8px" @click="previewRecommendationForm">预览上传文件</a-button>
                            <div style="width: 300px;">
                                <a-progress :percent="gzsc_percent" v-show="gzsc_percent_flag"/>
                            </div>
                        </span>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <!-- 查询区域-END -->

        <!-- 操作按钮区域 -->
        <div class="table-operator"></div>

        <!-- table区域-begin -->
        <div>
            <a-table
                ref="table"
                size="middle"
                :scroll="{x:true}"
                bordered
                rowKey="id"
                :columns="columns"
                :dataSource="dataSource"
                :pagination="ipagination"
                :loading="loading"
                class="j-table-force-nowrap"
                :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, getCheckboxProps: getCheckboxProps}"
                @change="handleTableChange">
                <span slot="cxj_tjzt" slot-scope="text, record">
                    {{record.tjzt}}
                    <!-- 1、成果状态为“已推荐”、“不推荐”的成果不能点击推荐按钮
                    2、不在推荐日期内的推荐单位不能推荐-->
                    <!-- v-if="record.tjztbh!='203'&&record.tjztbh!='202'&&record.tjztbh!='201'&&record.tjsj=='ok'" -->
                    <img
                        v-if="checkStatus(record)"
                        @click="handleUpdateStatus(record)"
                        style="width:13px;height:14px;cursor:pointer;"
                        src="../../assets/img/xszt.png"
                        alt=""
                    >
                </span>
                <span slot="action" slot-scope="text, record">
                    <a @click="viewDetail(record)">查看</a>
                </span>
            </a-table>
        </div>

        <cxj-tj-tjyj-modal ref="modalForm" @ok="modalFormOk"></cxj-tj-tjyj-modal>
        <!-- 预览 -->
        <preview-tjb :showPdf="showpdf"></preview-tjb>
        <!-- 批量推荐 -->
        <BatchRecommend :showFlag="batchFlag" :cxjIds="selectedRowKeys" @callback="callbackBatchRecommend" />
    </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import CxjTjTjyjModal from './modules/CxjTjTjyjModal'
import { ACCESS_TOKEN } from "@/store/mutation-types"
import Vue from 'vue'
import previewTjb from "./previewTjb"
import {
    exportTjcgXls,
    TjcgUpLoad
} from "@/api/login";
import BatchRecommend from './CxjTjTjyjList/index'
import {
    // 创新奖推荐-获取推荐单位信息
    cxjtjGetTjdwxx,
} from "@/api/api";
export default {
    name: 'CxjTjTjyjList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
        CxjTjTjyjModal,
        previewTjb,
        BatchRecommend
    },
    data () {
        return {
            showpdf: false,
            // queryParam: {},
            description: '成果推荐表管理页面',
            year: "",
            // 表头
            columns: [
                {
                    title: '序号',
                    align: "center",
                    dataIndex: '',
                    width: 50,
                    customRender: function (t,r,index) {
                        return parseInt(index)+1;
                    }
                },
                {
                    title:'成果编号',
                    align:"center",
                    dataIndex: 'cxj_xmbh',
                  width: 100,
                },
                {
                    title:'成果名称',
                    dataIndex: 'cxj_mc_zw',
                  width: 200,
                },

                {
                    title:'成果类别',
                    align:"center",
                    dataIndex: 'fl_id',
                  width: 100,
                    customRender: function(id){
                      return id == 11 ? '技术成果': id == 21?'系统成果':id == 31?'标准成果':id==41?'信息化成果':id==61?'职工成果':id==51?'专利成果':'无类别'
                    }
                },
                {
                    title:'申报单位',
                    dataIndex: 'sbdw_qymc',
                  width: 110,
                },
                {
                    title:'起始时间',
                    align:"center",
                    dataIndex: 'cxj_qssj',
                  width: 100,
                },
                {
                    title:'完成时间',
                    align:"center",
                    dataIndex: 'cxj_wcsj',
                  width: 100,
                },
                {
                    title:'完成人',
                    dataIndex: 'wcrlist',
                  width: 200,
                },
                {
                    title:'完成单位',
                    dataIndex: 'wcdwlist',
                    width: 200,
                },

                {
                    title:'推荐状态',
                    align:"center",
                    dataIndex: 'tjzt',
                    fixed: "right",
                    scopedSlots: { customRender: 'cxj_tjzt' },
                  width: 85,
                },
                {
                  title:'形审状态',
                  align:"center",
                  fixed: "right",
                  dataIndex: 'xszt',
                  width: 90,
                },
                {
                  title:'初审状态',
                  align:"center",
                  fixed: "right",
                  dataIndex: 'cszt',
                  width: 90,
                },
                {
                    title: '操作',
                    dataIndex: 'action',
                    align: "center",
                    fixed: "right",
                    width: 80,
                    scopedSlots: { customRender: 'action' }
                }
            ],
            url: {
                list: "/cxj/cxjTjTjyj/cxjgl_cxjTjList_tjdw",
                delete: "/cxj/cxjTjTjyj/delete",
                deleteBatch: "/cxj/cxjTjTjyj/deleteBatch",
                exportXlsUrl: "/cxj/cxjTjTjyj/exportXls",
                importExcelUrl: "cxj/cxjTjTjyj/importExcel",
                sbdwUrl: '/cxj/cxjSbSbdw/cxjsb_getSbtjdw',
            },
            dictOptions:{},
            superFieldList:[],
            fileStatus: "",
            gzsc_percent_flag:false,
            gzsc_percent: 0,
            selectedRowKeys: [],
            selectionRows: [],
            batchFlag: false
        }
    },
    beforeUpdate(){
        let nowTime = new Date().getTime();
        this.dataSource.map(item=>{
            item.tjsj = 'ok';
            item.type = 'tjdw';
            if (nowTime>item.tj_kssj && nowTime<item.tj_jzsj) {
                // console.log('可以推荐')
                item.tjsj = 'ok';
            } else {
                item.tjsj = 'no';
                // console.log('不可以推荐')
            }
        })
    },
    created() {
        this.$set(this.dictOptions, 'isValid', [{text:'是',value:'Y'},{text:'否',value:'N'}])
    },
    computed: {
        importExcelUrl: function(){
            return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
        },
    },
    watch: {
        // '$route':'getCxjtjGetTjdwxx'
    },
    activated(){
        this.getCxjtjGetTjdwxx();
    },
    methods: {
        checkStatus(record) {
            const { tjztbh, tjsj } = record;
            return tjztbh != '203' && tjztbh != '202' && tjztbh != '201' && tjsj === 'ok';
        },
        batchRecommend() {
            if (!this.selectedRowKeys.length) {
                this.$message.warning('请选择成果');
                return;
            }
            this.batchFlag = !this.batchFlag;
        },
        callbackBatchRecommend() {
            this.selectedRowKeys = [];
            this.loadData();
        },
        onSelectChange(selectedRowKeys, selectionRows) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectionRows = selectionRows;
        },
        getCheckboxProps(record) {
            return {
                props: {
                    disabled: !this.checkStatus(record)
                }
            };
        },
        getCxjtjGetTjdwxx(){
            cxjtjGetTjdwxx().then(res=>{
                if (res.code == 200) {
                    if (res.result.tjdwxx) {
                        if (res.result.tjdwxx.tjdwxxSfqr == 'N' || !res.result.tjdwxx.tjdwxxSfqr) {
                            this.$message.warning('请先在我的信息中完善推荐单位信息！');
                        }
                    } else {
                        this.$message.warning('请先在我的信息中完善推荐单位信息！');
                    }
                }
            })
        },
        handleUpdateStatus(record) {
            cxjtjGetTjdwxx().then(res=>{
                if (res.code == 200) {
                    if (!res.result.tjdwxx || !res.result.tjdwxx.tjdwxxSfqr || res.result.tjdwxx.tjdwxxSfqr == 'N') {
                        this.$message.warning('请先在我的信息中完善推荐单位信息！');
                    } else {
                        this.$refs.modalForm.edit(record);
                        this.$refs.modalForm.title = "编辑";
                        this.$refs.modalForm.disableSubmit = false;
                    }
                }
            })
        },
        initDictConfig(){},
        getSuperFieldList(){
            let fieldList=[];
            fieldList.push({type:'string',value:'cxj_xmbh',text:'成果编号',dictCode:''})
            fieldList.push({type:'string',value:'cxj_mc_zw',text:'成果名称',dictCode:''})
            fieldList.push({type:'string',value:'fl_id',text:'成果类别',dictCode:''})
            fieldList.push({type:'string',value:'sbdw_qymc',text:'申报单位',dictCode:''})
            fieldList.push({type:'string',value:'cxj_qssj',text:'起始时间',dictCode:''})
            fieldList.push({type:'string',value:'cxj_wcsj',text:'完成时间',dictCode:''})
            fieldList.push({type:'string',value:'wcrlist',text:'完成人',dictCode:''})
            fieldList.push({type:'string',value:'wcdwlist',text:'完成单位',dictCode:''})
            fieldList.push({type:'string',value:'tjzt',text:'推荐状态',dictCode:''})
            fieldList.push({type:'string',value:'cxzt',text:'初审状态',dictCode:''})
            fieldList.push({type:'switch',value:'isValid',text:'是否有效'})
            this.superFieldList = fieldList;
        },
        viewDetail(item){
            let token = Vue.ls.get(ACCESS_TOKEN);
            sessionStorage.setItem('token', token);
            sessionStorage.setItem('tjztbh', item.tjztbh);
             let routeData = this.$router.resolve({
                path: '/shenbao/declare',
                query: {
                    type: item.fl_id,
                    cxj_cxj_id: item.id,
                    isHead: 2,
                    from: 'zj',
                    tjztbh: item.tjztbh,
                    xmztbh: item.xmztbh,
                    cxj_xmbh: item.cxj_xmbh,
                    cxj_mc_zw: item.cxj_mc_zw
                }
            });
            window.open(routeData.href, '_blank');
        },
        exportTjcgXls() {
            let that = this;
            exportTjcgXls().then(res=>{
                that.downfn('推荐成果汇总表',res);
            })
        },
        // 预览推荐表
        previewRecommendationForm(){
            console.log('预览推荐表')
            this.showpdf = !this.showpdf;
        },
        TjcgUpLoad() {
            const choosefile = document.querySelector('#gzsc');
            choosefile.click();
        },
        handlegzsc() {
            let that = this;
            that.gzsc_percent = 0;
            that.gzsc_percent_flag = true;
            const files = document.getElementById('gzsc').files[0];
            if (files) {
                // if (files.size > 50*1024*1024) { // 此处判断上传文件的大小 , 单位为 b 故要乘以两个1024
                //     this.$message.warning('文件最大为50兆')
                //     return
                // }
                if(window.FileReader) {
                    var fr = new FileReader();
                    fr.onprogress = function(e) {
                        that.gzsc_percent = (e.loaded/files.size)*100;
                    };
                    fr.readAsDataURL(files);
                }
                let formData = new FormData();
                formData.append('file', files);
                TjcgUpLoad(formData).then(res=>{
                    let data = res.data;
                    that.gzsc_percent_flag = false;
                    if (res.code == '200') {
                        document.getElementById('gzsc').value = null;
                        that.$message.success('上传成功');
                    } else {
                        that.$message.error(data.message);
                    }
                }).catch(err=>{
                    console.log(err)
                    that.gzsc_percent_flag = false;
                })
            }
        },
        downfn(filename, res) {
            var blob = res;
            if (window.navigator.msSaveOrOpenBlob) {
                navigator.msSaveBlob(blob, filename);
            } else {
                var a = document.createElement('a');
                var url =  window.URL.createObjectURL(blob);
                a.href = url;
                a.download = filename+'.xls';
                document.body.appendChild(a);
                a.click();
                window.URL.revokeObjectURL(url);
            }
        },
    }
}
</script>
<style scoped lang="less">
    @import '~@assets/less/common.less';
    .table-operator {
        span {
            color: #252525;
            line-height: 20px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-size: 14px;
            display: inline-block;
            margin-right: 18px;
        }
    }
    /deep/.ant-table-thead > tr > th .ant-table-header-column {
      width:100%;
      text-align: center!important;
    }
    /deep/.ant-table-tbody > tr > td {
      word-break: break-all;
      white-space: normal;
      word-wrap:break-word;
      font-size:14px;
      padding:3px 3px!important;
    }
    /deep/.ant-table-tbody > tr > td span{
      font-size:14px;
    }
</style>